import * as THREE from 'three';
import { OrbitControls } from '../../node_modules/three/examples/jsm/controls/OrbitControls';

export default class InitThree {
    constructor(containerDom) {
        this.containerDom = containerDom;
        this.renderer;
        this.camera;
        this.scene;
        this.light;
        this.controls;
        this.width = window.innerWidth;
        this.height = window.innerHeight;
    };
    // 添加坐标系
    addAxesHelper() {
        const axesHelper = new THREE.AxesHelper(5);
        this.scene.add(axesHelper);
    };
    // 初始化渲染器
    initRenderer() {
        this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setSize(this.width, this.height);
        this.containerDom.appendChild(this.renderer.domElement);
    };
    // 初始化相机
    initCamera() {
        this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 10000);
        this.camera.position.set(20, 20, 20);
        this.camera.lookAt(0, 0, 0);
    };
    // 初始化场景
    initScene() {
        this.scene = new THREE.Scene();
        this.scene.background = new THREE.Color(0x020924);
        this.scene.fog = new THREE.Fog(0x020924, 200, 1000);
    };
    // 初始化用户交互
    initControls() {
        this.controls = new OrbitControls(this.camera, this.renderer.domElement);
        this.controls.enableDamping = true;
        this.controls.enableZoom = true;
        this.controls.autoRotate = false;
        this.controls.autoRotateSpeed = 2;
        this.controls.enablePan = true;
    };
    // 初始化光
    initLight() {
        const ambientLight = new THREE.AmbientLight(0xcccccc, 1.1);
        this.scene.add(ambientLight);
        var directionalLight = new THREE.DirectionalLight(0xffffff, 0.2);
        directionalLight.position.set(1, 0.1, 0).normalize();
        var directionalLight2 = new THREE.DirectionalLight(0xff2ffff, 0.2);
        directionalLight2.position.set(1, 0.1, 0.1).normalize();
        this.scene.add(directionalLight);
        this.scene.add(directionalLight2);
        var hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 0.2);
        hemiLight.position.set(0, 1, 0);
        this.scene.add(hemiLight);
        var directionalLight = new THREE.DirectionalLight(0xffffff);
        directionalLight.position.set(1, 500, - 20);
        directionalLight.castShadow = true;
        directionalLight.shadow.camera.top = 18;
        directionalLight.shadow.camera.bottom = - 10;
        directionalLight.shadow.camera.left = - 52;
        directionalLight.shadow.camera.right = 12;
        this.scene.add(directionalLight);
    };
    // 窗口变动
    onWindowResize() {
        this.camera.aspect = window.innerWidth / window.innerHeight;
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.renders();
    };
    // 渲染
    renders() {
        this.renderer.clear();
        this.renderer.render(this.scene, this.camera);
    };
    // 更新
    animate() {
        window.requestAnimationFrame(() => {
            if (this.controls) this.controls.update();
            this.renders();
            this.animate();
        });
    };
    // 初始化
    init() {
        this.initRenderer();
        this.initCamera();
        this.initScene();
        this.initLight();
        this.addAxesHelper();
        this.initControls();
        this.animate();
        window.addEventListener('resize', () => { return this.onWindowResize() }, false);
    }
}
